package com.yun.userdemo.controllers;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.yun.userdemo.beans.User;
import com.yun.userdemo.service.UserService;
import com.yun.userdemo.utils.Constants;
import com.yun.userdemo.utils.R;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController{

    Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    /**
     * 用户添加
     * @param user
     * @return
     */
    @PostMapping(value = "/add")
    public R addUser(@RequestBody User user){

        logger.info("UserController addUser=============>User "+user);

        if (user.getCreate_time() == null){
            user.setCreate_time(new Date());
        }

        if (user.getUpdate_time() == null){
            user.setUpdate_time(new Date());
        }

        Map<String,Object> result = new HashMap<>();
        int i = userService.addUser(user);
        if (i > 0){
            return R.ok();
        }else{
            return R.error(Constants.CODE_FAILD,Constants.CODE_FAILD_MSG);
        }
    }

    /**
     * 用户修改
     * @param user
     * @return
     */
    @PostMapping("/update")
    public R updateUser(@RequestBody User user){

        logger.info("UserController updateUser=============>User "+user);

        Map<String,Object> result = new HashMap<>();
        int i = userService.updateUser(user);
        if (i > 0){
            return R.ok();
        }else{
            return R.error(Constants.CODE_FAILD,Constants.CODE_FAILD_MSG);
        }
    }

    /**
     * 用户删除
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public R deleteUser(@PathVariable("id") Integer id){

        logger.info("UserController deleteUser=============>id "+id);

        Map<String,Object> result = new HashMap<>();
        int i = userService.deleteUserById(id);
        if (i > 0){
            return R.ok();
        }else{
            return R.error(Constants.CODE_FAILD,Constants.CODE_FAILD_MSG);
        }
    }

    /**
     * 根据用户id查询正常状态的用户
     * @param id
     * @return
     */
    @RequestMapping("/query/{id}")
    public Object queryUserById(@PathVariable("id") Integer id){

        logger.info("UserController queryUserById=============>id "+id);

        Map<String,Object> result = new HashMap<>();
        User user = userService.queryUserById(id);
        if (user != null){
            return R.ok().put("data",user);
        }else{
            return R.error(Constants.CODE_NOEXIST,Constants.CODE_NOEXIST_MSG);
        }
    }

    /**
     * 查询所有正常状态的用户
     * @return
     */
    @RequestMapping("/list")
    public R queryAllUser(){

        logger.info("UserController queryAllUser=============>");

        List<User> list = userService.queryAllUser();
        if (list != null && list.size() > 0){
            return R.ok().put("data",list);
        }else{
            return R.error(Constants.CODE_NULL,Constants.CODE_NULL_MSG);
        }
    }
}
